
package cz.geoserver.KMLpublisher.db.model;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;


/**
 * Class models current result table of incoming SQL query
 * From this class is than generated output KML 
 * @author mkepka
 *
 */
public class GeometryFeature {

    private List<Attribute> attributes;
    private String geomKML;
    
    private final String geomColName = "the_geom";
    private String geomAsKMLName = "asKML";
    private String geomSt_asKMLName = "st_asKML";
    
    /**
     * Constructor creates instance of this class
     */
    public GeometryFeature(){
    }

    /**
     * Constructor creates instance of this class from ResultSet object
     * @param set - ResultSet object with one row from database result table  
     * @throws SQLException - Throws SQLException if an exception occurs while getting values from ResultSet
     */
    public GeometryFeature (ResultSet set) throws SQLException{
        attributes = new LinkedList<Attribute>();
        Attribute att;
        String colName;
        String colVal;
        ResultSetMetaData rsmd = set.getMetaData();
        int colCount = rsmd.getColumnCount();
        for (int col = 1; col<=colCount; col++){
            colName = rsmd.getColumnName(col);
            if(colName.equalsIgnoreCase(geomAsKMLName)==true || colName.equalsIgnoreCase(geomSt_asKMLName)==true){
                this.geomKML=set.getString(col);
            }
            else{
                if(colName.equalsIgnoreCase(geomColName)==false){
                    colVal = set.getString(col);
                    att = new Attribute(colName, colVal);
                    attributes.add(att);
                }
            }
        }
    }

    /**
     * Getter returns LinkedList of Attribute classes that was in result table without columns asKML(...) or the_geom
     * @return the attributes - LinkedList of Attribute classes that was in result table
     */
    public List<Attribute> getAttributes() {
        return attributes;
    }

    /**
     * Getter returns name of column that contains geometry in KML format
     * @return the geomKML - name of column that contains geometry in KML format
     */
    public String getGeomKML() {
        return geomKML;
    }

    /**
     * Getter returns String with attributes names and values in form of KML ExtendedData element
     * @return - String with attributes names and values in form of KML ExtendedData element
     */
    public String getKMLExtendedData(){
        String exData = "";
        for (int i = 0; i<attributes.size(); i++){
            String exDataRow = "<Data name=\""+attributes.get(i).getAttName()+"\"><value>"+attributes.get(i).getAttValue()+"</value></Data>\n";
            exData = exData+exDataRow;
        }
        return exData;
    }

    /**
     * Method returns String of geometry column name and all column names
     */
    public String toString() {
        return "[" + geomKML + ", " + attributes.toString() + "]";
    }
}